import Vue from 'vue'
import Vuex from 'vuex'
//import { checkToken,User } from '@/config/api.js'// api 登录
import { set } from '@/utils/db.js';//缓存操作

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
	},
   mutations: {
		
	},
	actions: {
		//判断用户
		checkHandle(type,payload) {
			return new Promise((resolve,reject) => {
				checkToken().then(res => {
					if(res.Ret === 200) {
						resolve(res.Data)
					}else if(res.Ret === 408) {
						/* 无token或token过期 */
						uni.login({
						   success: result=> {
								User.wechatLog({
									Code:result.code
								}).then(res => {
									let token = res.Data.Authorization;
									set('access_token',token);
									let page = getCurrentPages()[0];
									if(payload.type=='load'){
										page.onLoad(payload.val);
									}else {
										page.onShow();
									}
								})
						  }
						});
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
		//获取用户的pv/uv 简易埋点
		// statistics(type,payload){
			// return new Promise ((resolve,reject)=>{
			// 	User.pageHistory(payload).then(res => {
			// 		if(res.Ret === 200){
			// 			resolve(res.Data)
			// 		}
			// 	}).catch(err => {
			// 		reject(err)
			// 	})
			// })
		// }
	},
	getters: {
	}
})

export default store
